#include <stdio.h>
//opendir 
#include <dirent.h>
#include <sys/types.h>
//exit
#include <stdlib.h>
//str-about
#include <string.h>
#include "DoubleList.h"

int bmpfind(char *dirname, DNodep headp)
{
		int bmpcnt = 0; //bmp图片的数量
		DIR *dp = NULL;
		struct dirent* entp = {0};
		DNodep tempp = NULL;

		char pathname[100] = {0};
		//1. open the dir
		dp = opendir(dirname);
		if(dp == NULL)
		{
				perror("dir open failed!");
				exit(-1);
		}
		//2. read the dir,one file per time
		while(1)
		{
			entp = readdir(dp);
			if(entp == NULL)
			{
				break;
			}
			
			//if((entp->d_type == DT_REG) && (strstr(entp->d_name, ".bmp") != NULL))
			if((strstr(entp->d_name, ".bmp") != NULL))
			{
				bmpcnt++;
				//printf("the bmp file found is %s\n", entp->d_name);
				//添加进链表里面
				//tempp = DoubleNode_Create(entp->d_name);
				memset(pathname, 100, 0);
				strcpy(pathname, dirname);
				strcat(pathname,"/");
				strcat(pathname, entp->d_name);
				tempp = DoubleNode_Create(pathname);
				if(tempp == NULL)
				{
					perror("DNode create failed!");
					exit(-1);
				}
				DoubleList_TailInsert(headp, tempp);
			}	
		}
		//3. check the file's attr and postname
		//if the postname is right add the ${bmpcnt} and printf it
		printf("the total bmpcnt is %d\n", bmpcnt);
		return bmpcnt;
}

